import {
    Violation
} from './violation.model'
import {
    Config
} from '../../utils/config'
import Toast from '@vant/weapp/toast/toast'
const violation = new Violation()
Page({

    /**
     * 页面的初始数据
     */
    data: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 提交查询
     * @param {*} options
     */
    doSubmit(options) {
        let that = this,
            errMsg = '',
            value = options.detail.value,
            preCarNum = '',
            param = JSON.stringify(value)
        if (!/^[京津沪渝]$/.test(value.carNumber.slice(0, 1))) {
            preCarNum = value.carNumber.slice(0, 2)
        } else {
            preCarNum = value.carNumber.slice(0, 1)
        }
        if (!value.carCode) {
            errMsg = '车架号不能为空'
        }
        if (value.carNumber && !Config.carNumber.test(value.carNumber)) {
            errMsg = '车牌号格式不正确'
        }
        if (!value.carNumber) {
            errMsg = '车牌号不能为空'
        }
        if (errMsg) {
            Toast(errMsg)
            return false
        }
        violation.getViolationRuleData({
            preCarNum: preCarNum
        }).then((res) => {
            if (!res.data.flag) {
                Toast.fail('不支持此地区')
                return false
            } else {
                let rule = res.data.data[0]
                if (value.carEngineCode && rule.carEngineLen != value.carEngineCode.length) {
                    errMsg = `发动机编号为${rule.carEngineLen}位`
                }
                if (value.carCode && rule.carCodeLen != value.carCode.length) {
                    errMsg = `车架号为${rule.carCodeLen}位`
                }
                if (errMsg) {
                    Toast(errMsg)
                    return false
                }
                wx.navigateTo({
                    url: `/pages/violation-detail/violation-detail?param=${param}`,
                })
            }
        })
    }
})